from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse, redirect
from app01 import models

class Tracer(object):
    def __init__(self):
        self.user = None
        self.level = None
        self.team_id = None

class AuthMiddleware(MiddlewareMixin):
    """中间件1"""

    def process_request(self, request):
        # #0.排除那些不需要登录就能访问的页面
        if request.path in ['/login/', '/image/code/','/register/']:
            return None
        request.tracer = Tracer()
        #1.读取当前访问的用户的session信息，如果没有，则跳转到登录页面，如果有，则跳转到首页
        info_dict=request.session.get('info')
        request.tracer.user=info_dict
        #获取当前用户的权限
        if info_dict:
            #获取当前用户的权限
            user_obj=models.StaffInfo.objects.filter(staff_num=info_dict['id']).first()
            request.tracer.level=user_obj.level
            request.tracer.team_id=user_obj.team_id
        # print(info_dict)
        if info_dict:
            return None
        return redirect('/login/')


